﻿(function () {

    layui.use(["layer", "jquery", "form", "tree"], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var tree = layui.tree;
        var shopManagerUserId = 0;

        $(document).ready(function () {
            shopManagerUserId = $("#hdShopManagerUserId").val();
            $("#btn-ViewUserStructure").click(onViewUserStructureBtnClick);
        });

        function onViewUserStructureBtnClick() {
            userId = shopManagerUserId;

            var layerId = layer.open({
                type: 1,
                title: "满堂创品",
                shade: [0.1, '#000'],
                skin: 'layui-layer-molv',
                area: ["350px", "80%"],
                offset: "10%",
                resize: false,
                content: '<div Id="ViewUserStructureDlg2">' + $("#ViewUserStructureDlg").html() + '</div>'
            });

            var loadingIndex = layer.msg(addStyle("正在加载 ......"),
                { icon: 16, shade: 0.01, offset: ["25%"], time: false });
            $.ajax("/User/QueryUserStructure", {
                method: "POST",
                data: "UserId=" + shopManagerUserId,
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.Success == true) {
                        handleData(data.Data);
                    } else {
                        layer.msg(addStyle("加载数据失败: " + data.Msg),
                            { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true }
                        );
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"),
                        { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                }
            });

            function handleData(data) {
                var treeData = Convert_2_TreeData(data, userId);
                var n = treeData.length;
                var dom = $("#ViewUserStructureDlg2 div").eq(0);
                tree.render({
                    elem: dom,
                    id: "Tree",
                    data: treeData
                });
            }

            function Convert_2_TreeData(arr, rootUserId) {
                var arr2 = arr.filter(item => item["Id"] == rootUserId);
                if (arr2.length > 0) {
                    var arr3 = [];
                    arr.forEach((item, i) => {
                        var arr4 = arr.filter(item2 => item["InvitationCode"] == item2["ReferenceInvitationCode"]);
                        item["children"] = arr4;
                        arr3.push(item);
                    });

                    var root = arr2[0];
                    var treeArr = [];
                    var treeObj = Convert_2_TreeItemData(root);
                    treeArr.push(treeObj);
                    fun1(treeObj["obj"]["InvitationCode"], treeObj["children"]);
                    return treeArr;
                    function fun1(invitationCode, parent) {
                        var arr4 = arr3.filter(x => x["ReferenceInvitationCode"] == invitationCode);
                        if (arr4.length > 0) {
                            for (var i = 0; i < arr4.length; i++) {
                                var item2 = arr4[i];
                                var treeObj2 = Convert_2_TreeItemData(item2);
                                parent.push(treeObj2);
                                fun1(item2["InvitationCode"], treeObj2["children"]);
                            }
                        }
                    }

                    function Convert_2_TreeItemData(obj) {
                        return {
                            id: obj["Id"],
                            title: obj["Name"] + " - " + obj["Phone"],
                            obj: obj,
                            children: []
                        };
                    }
                }
                return [];
            }
        }

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });

})();